﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace Text_Adventures
{
    public class Character :BaseCreature
    {

        public Character()
        {

        }
        public Character(StreamReader sr)
        {
            string line = "";

            while (!line.Equals("end"))
            {
                if (!sr.EndOfStream && ((line = sr.ReadLine().ToLower().Trim()) != ""))
                {
                    switch (line)
                    {
                        case "id":
                            line = sr.ReadLine();
                            switch (line)
                            {
                                case "basicnpc":
                                case "basicshop":
                                case "basictavern":
                                case "basicmedic":
                                case "basicbank":
                                    Name = Names[rand.Next(0, Names.Length - 1)];
                                    break;
                                default:
                                    break;
                            }
                            break;
                        case "str":
                            line = sr.ReadLine();
                            Str = Convert.ToInt32(line);
                            break;
                        case "int":
                            line = sr.ReadLine();
                            Int = Convert.ToInt32(line);
                            break;
                        case "agi":
                            line = sr.ReadLine();
                            Agi = Convert.ToInt32(line);
                            break;
                        case "vit":
                            line = sr.ReadLine();
                            Vit = Convert.ToInt32(line);
                            break;
                        default:
                            break;
                    }
                }
            }
        }

        public Character(StreamReader reader, string ID)
        {
            string line = "";

            while (!((line = reader.ReadLine()).Equals(ID))) ;

            this.ID = ID;
            switch (ID)
            {
                case "basicnpc":
                case "basicshop":
                case "basictavern":
                case "basicmedic":
                case "basicbank":
                    Name = Names[rand.Next(0, Names.Length - 1)];
                    break;
                default:                    
                    break;
            }

            while (!line.Equals("end"))
            {
                if (reader.EndOfStream)
                    break;
                if (!reader.EndOfStream && ((line = reader.ReadLine().ToLower().Trim()) != ""))
                {
                    switch (line)
                    {
                        case "name":
                            line = reader.ReadLine();
                            Name = line;
                            break;
                        case "str":
                            line = reader.ReadLine();
                            Str = Convert.ToInt32(line);
                            break;
                        case "int":
                            line = reader.ReadLine();
                            Int = Convert.ToInt32(line);
                            break;
                        case "agi":
                            line = reader.ReadLine();
                            Agi = Convert.ToInt32(line);
                            break;
                        case "vit":
                            line = reader.ReadLine();
                            Vit = Convert.ToInt32(line);
                            break;
                        default:
                            break;
                    }
                }
            }

            HP = rand.Next(8, 20 + ((Vit - 10) / 2));
            CHP = HP;
            MP = rand.Next(8, 20 + ((Int - 10) / 2));
            CMP = MP;
            Def = 0;
            Eva = (rand.Next(0, 100) + ((Agi - 10) / 2)*10)/10;
            Eva = Eva < 0 ? Eva*-1 : Eva;
            Acc = (70 + ((Agi - 10) / 2) * 10);
            Acc = Acc > 100 ? 100 : Acc;
            Money = 0;
            Level = 1;            
        }

        public void RandomRename()
        {
            Name = Names[rand.Next(0, Names.Length - 1)];
        }

        public Character Clone()
        {
            Character cloned = new Character();
            cloned.Str = this.Str;
            cloned.Int = this.Int;
            cloned.Agi = this.Agi;
            cloned.Vit = this.Vit;
            return cloned;
        }

    }
}
